package so.ifl.servcie.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.dao.OnlineExamDao;
import so.ifl.servcie.OnlineExamService;
import so.ifl.vo.OnlineExamVo;

/**
 *create by aaron.lu
 *create at 2013-9-14 下午9:39:42
 *<mailto: atyuan.lu@gmail.com>
 **/
@Service("onlineExamService")
public class OnlineExamServiceImpl implements OnlineExamService {
	
	@Autowired
	private OnlineExamDao onlineExamDao;
	
	private String duoxuan = "m";
	private String danxuan = "s";
	private String panduan = "j";
	
	/**
	 * 获取科目四模拟
	 * create by aaron.lu
	 * create at 2013-9-15 下午3:50:29
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	public String getSubject4Imitate(){
		String topics = "";
		List<OnlineExamVo> result = new ArrayList<OnlineExamVo>();
		
		Map<String, Object> paramsType1 = new HashMap<String, Object>();
		paramsType1.put("online_exam_subject", "4");
		paramsType1.put("online_exam_group", "i");
		
		List<Integer> jIds = onlineExamDao.queryIDWithParamAndPagination(paramsType1);
		paramsType1.put("online_exam_ids", shufflerAndGet(jIds, 20));
		
		List<OnlineExamVo> jlist = onlineExamDao.queryOnlineExamWithParamAndPagination(paramsType1);
		
		result.addAll(jlist);//判断题
		
		Map<String, Object> paramsType2 = new HashMap<String, Object>();
		paramsType2.put("online_exam_subject", "4");
		paramsType2.put("online_exam_group", "s");
		
		List<Integer> sIds = onlineExamDao.queryIDWithParamAndPagination(paramsType2);
		paramsType2.put("online_exam_ids", shufflerAndGet(sIds, 25));
		
		List<OnlineExamVo> slist = onlineExamDao.queryOnlineExamWithParamAndPagination(paramsType2);
		result.addAll(slist);//单选题
		
		Map<String, Object> paramsType3 = new HashMap<String, Object>();
		paramsType3.put("online_exam_subject", "4");
		paramsType3.put("online_exam_group", "m");
		
		List<Integer> mIds = onlineExamDao.queryIDWithParamAndPagination(paramsType3);
		paramsType3.put("online_exam_ids", shufflerAndGet(mIds, 5));
		
		List<OnlineExamVo> mlist = onlineExamDao.queryOnlineExamWithParamAndPagination(paramsType3);
		result.addAll(mlist);//多选
		
		
		topics = list2String(result);
		
		
		return topics;
	}
	
	/**
	 * 获取科目一模拟
	 * create by aaron.lu
	 * create at 2013-9-15 下午11:19:58
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	public String getSubject1Imitate() {
		String topics = "";
		List<OnlineExamVo> result = new ArrayList<OnlineExamVo>();
		
		Map<String, Object> paramsType1 = new HashMap<String, Object>();
		paramsType1.put("online_exam_subject", "1");
		paramsType1.put("online_exam_group", "i");
		
		List<Integer> jIds = onlineExamDao.queryIDWithParamAndPagination(paramsType1);
		paramsType1.put("online_exam_ids", shufflerAndGet(jIds, 40));
		
		List<OnlineExamVo> jlist = onlineExamDao.queryOnlineExamWithParamAndPagination(paramsType1);
		
		result.addAll(jlist);//判断题
		
		Map<String, Object> paramsType2 = new HashMap<String, Object>();
		paramsType2.put("online_exam_subject", "1");
		paramsType2.put("online_exam_group", "s");
		
		List<Integer> sIds = onlineExamDao.queryIDWithParamAndPagination(paramsType2);
		paramsType2.put("online_exam_ids", shufflerAndGet(sIds, 60));
		
		List<OnlineExamVo> slist = onlineExamDao.queryOnlineExamWithParamAndPagination(paramsType2);
		result.addAll(slist);//单选题		
		
		topics = list2String(result);
		
		
		return topics;
	}
	
	/**
	 * 获取单条题目
	 * create by aaron.lu
	 * create at 2013-9-16 上午12:12:04
	 * <mailto: atyuan.lu@gmail.com>
	 * @param subject
	 * @param onlineExamId
	 * @return
	 *
	 */
	public OnlineExamVo getOnlineExamVo(Integer subject, Integer onlineExamId){
		OnlineExamVo onlineExamVo = null;
		if (null != subject && null != onlineExamId) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("online_exam_id", onlineExamId);
			List<OnlineExamVo> list = onlineExamDao.queryOnlineExamJustOne(params);
			if (null != list && list.size() > 0) {
				onlineExamVo = list.get(0);
			}
		}
		return onlineExamVo;
	}
	
	/**
	 * 转换成js数组
	 * create by aaron.lu
	 * create at 2013-9-15 下午3:59:29
	 * <mailto: atyuan.lu@gmail.com>
	 * @param list
	 * @return
	 *
	 */
	private String list2String(List<OnlineExamVo> list){
		String result = "";
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				result = result + list.get(i).toString();
				
				if (i != (list.size() - 1)) {
					result = result + ",";
				}
			}
		}
		
		return result;
	}
	
	/**
	 * 打乱顺序并返回需要的物理ID集合
	 * create by aaron.lu
	 * create at 2013-9-15 下午3:47:58
	 * <mailto: atyuan.lu@gmail.com>
	 * @param list
	 * @param need
	 * @return
	 *
	 */
	private List<Integer> shufflerAndGet(List<Integer> list, Integer need){
		List<Integer> result = new ArrayList<Integer>();
		if (null != list && null != need) {
			if (list.size() > need) {
				Collections.shuffle(list);
				
				for (int i = 0; i < need; i++) {
					result.add(list.get(i));
				}
			}
		}
		
		return result;
	}
}
